package domain;

import java.util.*;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;



@XmlRootElement(name = "game")
public class Game {
	private int gameID;
	private Date date;
	private int[] result;
	/*TODO
	 * Refactoring naar => Player[]
	 */
	private Player playerWhite;
	private Player playerBlack;

	
	public Game(int gameID, Date date, Player playerWhite, Player playerBlack) {
		setGameID(gameID);
		setDate(date);
		setPlayerWhite(playerWhite);
		setPlayerBlack(playerBlack);
		result = new int[2]; result[0] = Integer.MAX_VALUE; result[1] = Integer.MAX_VALUE;
		
		//TODO Part if each Player knows its games
		this.playerWhite.getGames().add(this);
		this.playerBlack.getGames().add(this);
	}
	
	public Game(){};

	/**
	 * This method is called by the Tournament.saveResult(int GameID, String result[]) method, to update the player stats.
	 * @param result  This will affect the attribute Player.amountOfPoints.
	 */
	public void updatePlayerStats(String[] stringResult) {
		int[] result = stringResultToNumerical(stringResult);
		playerWhite.setAmountOfPoints(playerWhite.getAmountOfPoints()+result[0]);
		playerBlack.setAmountOfPoints(playerBlack.getAmountOfPoints()+result[1]);
		playerWhite.setAmountOfGamesPlayed(playerWhite.getAmountOfGamesPlayed()+1);
		playerBlack.setAmountOfGamesPlayed(playerBlack.getAmountOfGamesPlayed()+1);
		playerWhite.setWhiteBlackBalance(playerWhite.getWhiteBlackBalance()+1);
		playerBlack.setWhiteBlackBalance(playerBlack.getWhiteBlackBalance()-1);
	}
	
	/*protected boolean isGameWithPlayers(Player lowestPlayer, Player higherPlayer)
	{
		boolean isGameWithPlayers = false;
		if((playerWhite.isSamePlayer(lowestPlayer) && playerBlack.isSamePlayer(higherPlayer))
				|| (playerWhite.isSamePlayer(higherPlayer) && playerWhite.isSamePlayer(lowestPlayer)))
			isGameWithPlayers = true;
		return isGameWithPlayers;
	}*/
	
	protected boolean isGameAgainstPlayer(Player targetPlayer)
	{
		boolean isGameAgainstPlayer = false;
		if(playerWhite.isSamePlayer(targetPlayer) || playerBlack.isSamePlayer(targetPlayer))
			isGameAgainstPlayer = true;
		return isGameAgainstPlayer;
	}
	
	public boolean isGameInProgress() {
		boolean isGameInProgress = false;
		if(result[0] == Integer.MAX_VALUE && result[1] == Integer.MAX_VALUE)
			isGameInProgress = true;
		return isGameInProgress;
	}
	
	public String toString()
	{
		return "[" + playerWhite.toString() + " VS " + playerBlack.toString() + "]";
	}
	
	public String[] toArray()
	{
		String[] gameInformation = new String[3];
		
		gameInformation[0] = playerWhite.toString();
		gameInformation[1] = playerBlack.toString();
		gameInformation[2] = "1-0@0,5-0,5@0-1@Niet gespeeld";
		gameInformation[3] = date.toString();
		gameInformation[4] = String.valueOf(gameID);
		
		return gameInformation;
	}
	
	private int[] stringResultToNumerical(String[] stringResult)
	{
		int[] numericalResult = new int[2];
		numericalResult[0] = Integer.valueOf(stringResult[0]);
		numericalResult[1] = Integer.valueOf(stringResult[1]);
		return numericalResult;
	}
	
	// SETTERS
	public void setDate(Date date) {
		this.date = date;
	}

	public void setGameID(int gameID) {
		this.gameID = gameID;
	}

	public void setResult(int[] result) {
		this.result = result;
	}

	public void setPlayerWhite(Player playerWhite) {
		this.playerWhite = playerWhite;
	}

	public void setPlayerBlack(Player playerBlack) {
		this.playerBlack = playerBlack;
	}

	// GETTERS
	@XmlElement(name = "gameID")
	public int getGameID() {
		return gameID;
	}

	@XmlElement(name = "date")
	public Date getDate() {
		return date;
	}

	@XmlElement(name = "result")
	public int[] getResult() {
		return result;
	}

	public Player getPlayerWhite() {
		return playerWhite;
	}

	public Player getPlayerBlack() {
		return playerBlack;
	}
}